package com.jn.rxjava.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log; // 导入Log类
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jn.rxjava.R;
import com.jn.rxjava.service.NewsResponse;

import java.util.List;

public class NewsListActivity extends AppCompatActivity {
    private ListView newsListView;
    private List<NewsResponse.Result.Data> newsData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);

        // 初始化 ListView
        newsListView = findViewById(R.id.news_list_view);
        newsData = (List<NewsResponse.Result.Data>) getIntent().getSerializableExtra("newsData");

        if (newsData != null) {
            Log.d("NewsListActivity", "成功获取新闻数据: " + newsData.size() + " 条记录");
            // 创建并设置适配器
            ArrayAdapter<NewsResponse.Result.Data> adapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_list_item_1, newsData);
            newsListView.setAdapter(adapter);

            // 设置列表项点击事件
            newsListView.setOnItemClickListener((parent, view, position, id) -> {
                NewsResponse.Result.Data newsItem = newsData.get(position);
                Intent intent = new Intent(NewsListActivity.this, NewsDetailActivity.class);
                intent.putExtra("url", newsItem.getUrl());
                startActivity(intent);
            });
        } else {
            String errorMessage = "没有新闻数据";
            Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
            Log.e("NewsListActivity", errorMessage); // 添加日志
        }
    }
}
